package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.platform.common.Result;
import com.education.platform.dto.feedback.FeedbackCreateDTO;
import com.education.platform.dto.feedback.FeedbackReplyDTO;
import com.education.platform.entity.Feedback;
import com.education.platform.service.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 反馈控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "反馈管理", description = "反馈管理相关接口")
@RestController
@RequestMapping("/api/feedback")
@RequiredArgsConstructor
public class FeedbackController {

    private final FeedbackService feedbackService;

    @Operation(summary = "提交反馈", description = "提交新反馈")
    @PostMapping
    @SaCheckLogin
    public Result<Long> createFeedback(@Validated @RequestBody FeedbackCreateDTO feedbackCreateDTO) {
        Long feedbackId = feedbackService.createFeedback(feedbackCreateDTO);
        return Result.success(feedbackId);
    }

    @Operation(summary = "更新反馈", description = "更新反馈信息")
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<Void> updateFeedback(@PathVariable Long id, 
                                      @Validated @RequestBody FeedbackCreateDTO feedbackCreateDTO) {
        feedbackService.updateFeedback(id, feedbackCreateDTO);
        return Result.success();
    }

    @Operation(summary = "删除反馈", description = "删除反馈")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deleteFeedback(@PathVariable Long id) {
        feedbackService.deleteFeedback(id);
        return Result.success();
    }

    @Operation(summary = "获取反馈详情", description = "根据ID获取反馈详细信息")
    @GetMapping("/{id}")
    @SaCheckLogin
    public Result<Feedback> getFeedbackDetail(@PathVariable Long id) {
        Feedback feedback = feedbackService.getFeedbackById(id);
        return Result.success(feedback);
    }

    @Operation(summary = "分页查询反馈列表", description = "分页查询反馈列表（管理员）")
    @GetMapping("/page")
    @SaCheckRole("ADMIN")
    public Result<IPage<Feedback>> getFeedbackPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String priority) {
        Page<Feedback> page = new Page<>(current, size);
        IPage<Feedback> result = feedbackService.getFeedbackPage(page, type, status, priority);
        return Result.success(result);
    }

    @Operation(summary = "获取我的反馈列表", description = "获取当前用户的反馈列表")
    @GetMapping("/my")
    @SaCheckLogin
    public Result<List<Feedback>> getMyFeedbacks() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Feedback> feedbacks = feedbackService.getMyFeedbacks(userId);
        return Result.success(feedbacks);
    }

    @Operation(summary = "回复反馈", description = "管理员回复反馈")
    @PostMapping("/{id}/reply")
    @SaCheckRole("ADMIN")
    public Result<Void> replyFeedback(@PathVariable Long id, 
                                     @Validated @RequestBody FeedbackReplyDTO feedbackReplyDTO) {
        feedbackService.replyFeedback(id, feedbackReplyDTO);
        return Result.success();
    }

    @Operation(summary = "更新反馈状态", description = "更新反馈状态（管理员）")
    @PutMapping("/{id}/status")
    @SaCheckRole("ADMIN")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        feedbackService.updateStatus(id, status);
        return Result.success();
    }

    @Operation(summary = "获取待处理反馈", description = "获取待处理的反馈列表（管理员）")
    @GetMapping("/pending")
    @SaCheckRole("ADMIN")
    public Result<List<Feedback>> getPendingFeedbacks() {
        List<Feedback> feedbacks = feedbackService.getPendingFeedbacks();
        return Result.success(feedbacks);
    }

    @Operation(summary = "获取高优先级反馈", description = "获取高优先级反馈列表（管理员）")
    @GetMapping("/high-priority")
    @SaCheckRole("ADMIN")
    public Result<List<Feedback>> getHighPriorityFeedbacks() {
        List<Feedback> feedbacks = feedbackService.getHighPriorityFeedbacks();
        return Result.success(feedbacks);
    }

    @Operation(summary = "关闭反馈", description = "关闭反馈（管理员）")
    @PostMapping("/{id}/close")
    @SaCheckRole("ADMIN")
    public Result<Void> closeFeedback(@PathVariable Long id) {
        feedbackService.closeFeedback(id);
        return Result.success();
    }
}

